package com.example.swaggerDemo.controller;


import com.example.swaggerDemo.entity.MsgEntity;
import com.example.swaggerDemo.entity.SysUserEntity;
import com.example.swaggerDemo.entity.WuliaoEntity;
import com.example.swaggerDemo.service.UserService;
import com.example.swaggerDemo.service.WuliaoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.List;

@Api("物料服务")
@RestController
@RequestMapping(value="/Wuliao")
public class WuliaoController {
    @Autowired
    private WuliaoService Service;

    @Autowired
    private UserService SysUserDao;
    @ApiOperation(value="申请物料",notes="申请物料")
    @ResponseBody
    @RequestMapping(value="/insert",method= RequestMethod.POST)
    public  Object insert(@RequestParam("userid") String userid,
                          @RequestParam("Nickname") String Nickname,
                          @RequestParam("WuliaoA") Integer WuliaoA,
                          @RequestParam("WuliaoB") Integer WuliaoB,
                          @RequestParam("phone") String phone,
                           @RequestParam("address") String address
    ) throws SQLException {
        int dto = 0;
        MsgEntity msg = new MsgEntity();
        SysUserEntity user = SysUserDao.findByid(userid);
        if (WuliaoA !=0 && WuliaoA != null) {
            dto = Service.insert(userid, Nickname, "0", WuliaoA.toString(),phone,address);
        }
        if (WuliaoB !=0 && WuliaoB != null) {
            dto = Service.insert(userid, Nickname, "1", WuliaoB.toString(),phone,address);
        }
        if (dto > 0) {
            msg.msg = "添加成功";
            msg.code = true;
            msg.object = dto;
        } else {
            msg.msg = "添加失败";
            msg.code = false;
            msg.object = dto;
        }
        return msg;
    }

    @ApiOperation(value="通过用户id查询该用户下的物料信息",notes="通过用户id查询该用户下的物料信息")
    @ResponseBody
    @RequestMapping(value="/findbyUserId",method= RequestMethod.POST)
    public  Object findbyUserId(@RequestParam("userid") String userid,
                                    @RequestParam("pageindex") int pagindex,
                                    @RequestParam("pagesize") int pagesize) throws SQLException
    {
        MsgEntity msg=new MsgEntity();
        try {
            List<WuliaoEntity> dto = Service.findUserId(userid,pagindex,pagesize);
            if ( dto.size()>0){


                msg.msg="数据查询成功";
                msg.code=true;
                msg.object=dto;
            }
            else
            {
                msg.msg="没有查询导数据";
                msg.code=false;
                msg.object=dto;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return msg;
    }

    @ApiOperation(value="管理员查询用户下的物料信息",notes="管理员查询用户下的物料信息")
    @ResponseBody
    @RequestMapping(value="/findadmin",method= RequestMethod.POST)
    public  Object findadmin(@RequestParam("status") String status,
                                @RequestParam("pageindex") int pagindex,
                                @RequestParam("pagesize") int pagesize) throws SQLException
    {
        MsgEntity msg=new MsgEntity();
        try {
            List<WuliaoEntity> dto = Service.findadmin(status,pagindex,pagesize);
            if ( dto.size()>0){
                msg.msg="数据查询成功";
                msg.code=true;
                msg.object=dto;
            }
            else
            {
                msg.msg="没有查询导数据";
                msg.code=false;
                msg.object=dto;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return msg;
    }

    @ApiOperation(value="管理员发货",notes="管理员发货")
    @ResponseBody
    @RequestMapping(value="/updawuliao",method= RequestMethod.POST)
    public  Object updawuliao(
            @RequestParam("id") String id,
            @RequestParam("status") String status,
                    @RequestParam("userid") String userid) throws SQLException
    {
        MsgEntity msg=new MsgEntity();
        try {
           int dto = Service.updatewuliaoType(id,status);
            if ( dto>0){

                WuliaoEntity wulaio = Service.findByid(id);
                SysUserEntity user = SysUserDao.findByid(userid);
                if (wulaio.getType().equals("0")) {
                    SysUserDao.updateMateriel(user.getMaterielA() + Integer.parseInt(wulaio.getAmount()), user.getMaterielB(), userid);
                }
                if (wulaio.getType().equals("1")) {
                    SysUserDao.updateMateriel(user.getMaterielA(), user.getMaterielB() + Integer.parseInt(wulaio.getAmount()), userid);
                }
                msg.msg="数据查询成功";
                msg.code=true;
                msg.object=dto;
            }
            else
            {
                msg.msg="没有查询导数据";
                msg.code=false;
                msg.object=dto;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return msg;
    }
}
